package com.java1412.controller;

import com.java1412.entity.Users;
import com.java1412.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author java1412
 * @create 2021-05-20 10:36
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("")
    public int addUser(@RequestBody Users users) {
        return userService.add(users);
    }

    @PutMapping("/{id}")
    public String updateUser(@PathVariable Integer id, @RequestBody Users users) {
        Users oldUser = new Users();
        oldUser.setId(id);
        oldUser.setUsername(users.getUsername());
        oldUser.setPassword(users.getPassword());
        int result = userService.update(oldUser);
        if (result == 1) {
            return users.toString();
        } else {
            return "更新学生信息错误";
        }
    }

    @GetMapping("/{id}")
    public Users findUserById(@PathVariable Integer id) {
        return userService.findUserById(id);
    }

    @GetMapping("/list")
    public List<Users> findUserList() {
        return userService.findUserList();
    }
}
